package com.housediyadmin.view.titlewindow
{   	
	
	import com.housediyadmin.events.bubbleEvents.UploadDoneEvent;
	import com.housediyadmin.events.picModelEvent.AddPicturesEvent;
	import com.housediyadmin.model.GoodsModel;
	import com.housediyadmin.model.PicModel;
	import com.housediyadmin.model.PictureModel;
	import com.housediyadmin.view.components.UploadPicCompMediator;
	import com.housediyadmin.vo.Goods;
	import com.housediyadmin.vo.Picture;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/2/15
	 */ 
	public class UploadGoodsPicMediator extends Mediator
	{
		[Inject]
		public var view:UploadGoodsPicView;
		
		[Inject]
		public var picModel:PicModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
		
		[Inject]
		public var pictureModel:PictureModel;
		
		
		[Bindable]
		public var checkArray:Array = new Array();
		override public function onRegister():void
		{		
			view.currentState = "upload";
			//为组件UploadPicComp手动创建Mediator
			mediatorMap.createMediator(view.uploadPic);

			view.previewBtn.addEventListener(MouseEvent.CLICK, prePopUpHandler);
			view.nextBtn.addEventListener(MouseEvent.CLICK, nextPopUpHandler);
			view.cancleBtn.addEventListener(MouseEvent.CLICK, closeHandler);
			view.addEventListener(CloseEvent.CLOSE, closeHandler);
					
			//监听图片上传完的事件，将view切换到另一状态
			view.uploadPic.addEventListener(UploadDoneEvent.UPLOAD_DONE, uploadDoneHandler);
		}
		
		private function prePopUpHandler(event:MouseEvent):void
		{
			mediatorMap.removeMediatorByView(view.uploadPic);
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );
			var popUp:FittingBrandEditView = new FittingBrandEditView();
			PopUpManager.addPopUp(popUp, view, true);
			mediatorMap.createMediator(popUp);
			PopUpManager.centerPopUp(popUp);
		}
		
		private function nextPopUpHandler(event:MouseEvent):void
		{		
			if(picModel.addPicArray.length != picModel.selectedFiles.length)
			{
				Alert.show("请设置图片属性");
			}
			else
			{
				dispatch(new AddPicturesEvent(AddPicturesEvent.ADD_PICTURES, picModel.addPicArray));
				mediatorMap.removeMediatorByView(view.uploadPic);
				PopUpManager.removePopUp(view);
				mediatorMap.removeMediator( this );
				var popUp:SelectSizeView = new SelectSizeView();
				PopUpManager.addPopUp(popUp, view, true);
				mediatorMap.createMediator(popUp);
				PopUpManager.centerPopUp(popUp);
			}
		}
		
		private function closeHandler(event:Event):void
		{
			mediatorMap.removeMediatorByView(view.uploadPic);
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );
		}
		
		private function uploadDoneHandler(event:UploadDoneEvent):void
		{
			view.currentState = "done";
			view.showPic.currentState = "base";
			mediatorMap.removeMediatorByView(view.uploadPic);
			mediatorMap.createMediator(view.showPic);
		}
	}
}